package com.vf.admin.common.freemarker;


import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
@Component
public class MapStringString implements TemplateDirectiveModel {

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
                        TemplateDirectiveBody body) throws TemplateException, IOException {
        Map map = null;
        Object key = null;
        if (params != null && params.get("map") != null) {
            SimpleHash simHash = (params.get("map") !=null ? (SimpleHash)(params.get("map")) : new SimpleHash());
            map = simHash.toMap();
        }else{
            map = new HashMap();
        }

        if (params != null && params.get("key") != null) {
            key = (params.get("key") !=null ? params.get("key").toString() : null);
        }
        String value = map.get(key)!=null ? map.get(key).toString() : "";

        // 真正开始处理输出内容
        Writer out = env.getOut();
        out.write(value);
    }

}
